package cn.iocoder.yudao.module.system.dal.mysql.examclass;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.system.controller.admin.examclass.vo.ExamClassPageReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.examclass.ExamClassDO;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;
import java.util.Set;

/**
 * 班级信息 Mapper
 *
 * @author 芋道源码
 */
@Mapper
public interface ExamClassMapper extends BaseMapperX<ExamClassDO> {

    default PageResult<ExamClassDO> selectPage(ExamClassPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<ExamClassDO>()
                .likeIfPresent(ExamClassDO::getClassName, reqVO.getClassName())
                .eqIfPresent(ExamClassDO::getClassCode, reqVO.getClassCode())
                .eqIfPresent(ExamClassDO::getGrade, reqVO.getGrade())
                .eqIfPresent(ExamClassDO::getTeacherId, reqVO.getTeacherId())
                .eqIfPresent(ExamClassDO::getStatus, reqVO.getStatus())
                .betweenIfPresent(ExamClassDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(ExamClassDO::getId));
    }

    default List<ExamClassDO> selectList(ExamClassPageReqVO reqVO) {
        return selectList(new LambdaQueryWrapperX<ExamClassDO>()
                .likeIfPresent(ExamClassDO::getClassName, reqVO.getClassName())
                .eqIfPresent(ExamClassDO::getClassCode, reqVO.getClassCode())
                .eqIfPresent(ExamClassDO::getGrade, reqVO.getGrade())
                .eqIfPresent(ExamClassDO::getTeacherId, reqVO.getTeacherId())
                .eqIfPresent(ExamClassDO::getStatus, reqVO.getStatus())
                .betweenIfPresent(ExamClassDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(ExamClassDO::getId));
    }

    default ExamClassDO selectByClassCode(String classCode) {
        return selectOne(ExamClassDO::getClassCode, classCode);
    }

    default List<ExamClassDO> selectListByTeacherId(Long teacherId) {
        return selectList(ExamClassDO::getTeacherId, teacherId);
    }

    default List<ExamClassDO> selectListByGrade(String grade) {
        return selectList(ExamClassDO::getGrade, grade);
    }

    /**
     * 根据班级名称查询班级
     */
    default ExamClassDO selectByClassName(String className) {
        return selectOne(ExamClassDO::getClassName, className);
    }

    /**
     * 根据班级名称和部门ID查询班级（用于避免不同部门的同名班级冲突）
     */
    default ExamClassDO selectByClassNameAndDeptId(String className, Long deptId) {
        return selectOne(new LambdaQueryWrapperX<ExamClassDO>()
                .eq(ExamClassDO::getClassName, className)
                .eq(ExamClassDO::getDeptId, deptId));
    }

    /**
     * 根据部门ID集合查询班级列表
     */
    default List<ExamClassDO> selectListByDeptIds(Set<Long> deptIds) {
        return selectList(new LambdaQueryWrapperX<ExamClassDO>()
                .in(ExamClassDO::getDeptId, deptIds)
                .orderByDesc(ExamClassDO::getId));
    }

    /**
     * 批量查询班级（根据班级名称列表）
     */
    default List<ExamClassDO> selectByClassNames(List<String> classNames) {
        if (classNames == null || classNames.isEmpty()) {
            return new java.util.ArrayList<>();
        }
        return selectList(new LambdaQueryWrapperX<ExamClassDO>()
                .in(ExamClassDO::getClassName, classNames));
    }

}